package cn.dy.workatten.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import cn.dy.base.framework.db.DBConnectPool;
import cn.dy.base.framework.db.DBUtil;
import cn.dy.base.framework.db.util.DBExecuteUtil;
import cn.dy.workatten.pojo.AttendRecord;
import cn.dy.workatten.pojo.AuditingDetail;

public class AuditingDetailDao {

	protected static Logger logger = LoggerFactory.getLogger(AuditingDetailDao.class);

	private DataSource dataSource;
	private JdbcTemplate jdbcTemplate;
	private NamedParameterJdbcTemplate namedJdbcTemplate;

	public AuditingDetailDao() {
		this.dataSource = DBConnectPool.create().getDataSource();
		if (this.jdbcTemplate == null)
			this.jdbcTemplate = new JdbcTemplate(this.dataSource);
		if (this.namedJdbcTemplate == null) {
			this.namedJdbcTemplate = new NamedParameterJdbcTemplate(this.dataSource);
		}
	}
	
	/**
	 * 获得自增系列
	 * @return
	 */
	public long getUniqueId() {
		long result = 0L;
		try {
			result = DBUtil.getSequence(new JdbcTemplate(this.dataSource), "seq_bz_auditing_detail");
		} catch (DataAccessException e) {
			logger.error("获得审核环节明细自动生成的ID值失败", e);
		}
		return result;
	}

	/**
	 * 新增
	 * 
	 * @param scheduleCfg
	 * @return
	 */
	public int addAuditingDetail(AuditingDetail auditingDetail) {
		DBExecuteUtil db = new DBExecuteUtil();
		int result = 0;

		try {
			String sql = "insert into bz_auditing_detail(id,flow_id,pre_id,audit_id,audit_time,audit_remark,audit_sts,create_time) values(:id,:flow_id,:pre_id,:audit_id,:audit_time,:audit_remark,audit_sts,create_time)";

			Map resultMap = new HashMap();
//			resultMap.put("id", getUniqueId());
			resultMap.put("id", auditingDetail.getId());
			resultMap.put("flow_id", auditingDetail.getFlow_id());
			resultMap.put("pre_id", auditingDetail.getPre_id());
			resultMap.put("audit_id", auditingDetail.getAudit_id());
			resultMap.put("audit_time", auditingDetail.getAudit_time());
			resultMap.put("audit_remark", auditingDetail.getAudit_remark());
			resultMap.put("audit_sts", auditingDetail.getAudit_sts());
			resultMap.put("create_time", auditingDetail.getCreate_time());
			result = db.executeSql(sql, resultMap);
		} catch (DataAccessException e) {
			logger.error("添加审核环节明细失败", e);
		}
		return result;
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public int updateAuditingDetail(AuditingDetail auditingDetail) {
		DBExecuteUtil db = new DBExecuteUtil();
		int result = 0;
		try {
			String sql = "update bz_auditing_detail set flow_id=:flow_id,pre_id=:pre_id,audit_id=:audit_id,audit_time=:audit_time,audit_remark=:audit_remark,audit_sts=:audit_sts,create_time=:create_time where id=:id";

			Map resultMap = new HashMap();
			resultMap.put("id", Long.valueOf(auditingDetail.getId()));
			resultMap.put("flow_id", auditingDetail.getFlow_id());
			resultMap.put("pre_id", auditingDetail.getPre_id());
			resultMap.put("audit_id", auditingDetail.getAudit_id());
			resultMap.put("audit_time", auditingDetail.getAudit_time());
			resultMap.put("audit_remark", auditingDetail.getAudit_remark());
			resultMap.put("audit_sts", auditingDetail.getAudit_sts());
			resultMap.put("create_time", auditingDetail.getCreate_time());
			result = db.executeSql(sql, resultMap);
		} catch (DataAccessException e) {
			logger.error("修改审核环节明细失败", e);
		}
		return result;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	public int deleteAuditingDetailById(long id) {
		DBExecuteUtil db = new DBExecuteUtil();
		int result = 0;
		try {
			String sql = " delete from bz_auditing_detail where id=:id";
			Map resultMap = new HashMap();
			resultMap.put("id", Long.valueOf(id));
			result = db.executeSql(sql, resultMap);
		} catch (DataAccessException e) {
			logger.error("删除审核环节明细失败", e);
		}
		return result;
	}
	
	
	/**
	 * 删除某个审批流程的审核过程
	 * 
	 * @param id
	 * @return
	 */
	public int deleteAuditingDetailByFlowId(long flow_id) {
		DBExecuteUtil db = new DBExecuteUtil();
		int result = 0;
		try {
			String sql = " delete from bz_auditing_detail where flow_id=:flow_id";
			Map resultMap = new HashMap();
			resultMap.put("flow_id", Long.valueOf(flow_id));
			result = db.executeSql(sql, resultMap);
		} catch (DataAccessException e) {
			logger.error("删除审核环节明细失败", e);
		}
		return result;
	}
	

	/**
	 * 根据id查询审核环节明细信息
	 * 
	 * @param id
	 * @return
	 */
	public AuditingDetail getAuditingDetailById(long id) {
		DBExecuteUtil db = new DBExecuteUtil();
		AuditingDetail auditingDetail = new AuditingDetail();
		try {
			String sql = "select * from bz_auditing_detail where id=:id ";
			
			Map parameters = new HashMap();
			parameters.put("id", Long.valueOf(id));
			auditingDetail = (AuditingDetail) db.queryObject(sql, parameters, AuditingDetail.class);
		} catch (DataAccessException e) {
			logger.error("根据id获得审核环节明细失败", e);
		}
		return auditingDetail;
	}
	
	
	/**
	 *	 查询某个流程的所有审核环节明细列表
	 * 
	 * @param flow_id
	 * @return
	 */
	public List<AuditingDetail> getAuditingDetailList(long flow_id) {
		DBExecuteUtil db = new DBExecuteUtil();
		String sql = "select a.*, b.NAME AS auditName from bz_auditing_detail a, ecc_contact_info b where a.flow_id=:flow_id and a.audit_id = b.id order by a.pre_id asc";

		Map<String, Object> params = new HashMap();
		params.put("flow_id", Long.valueOf(flow_id));
		List<AuditingDetail> list = db.queryList(sql, params, AuditingDetail.class);
		return list;
	}
	
	
}
